/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package football;


import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.sql.DataSource;

/**
 *
 * @author ACER
 */
@Stateless
@LocalBean
@WebService
public class Drehkreuz {
       @PersistenceContext(unitName = "football-ejbPU")
    private EntityManager em;

   
    /**
     *
     * @param object
     */
    public void persist(Object object) {
        em.persist(object);
            }

 //  public Ticket getTicketIdBySitzplatz(int Sitzplatz){
        
   //    return em.createNamedQuery("Ticket.findByIdSitzplatz",Ticket.class).getParameter("idSitzplatz",Sitzplatz);
     
  //  }
    

        /**
     *
     * @return
     */
    public String getTicketId(){
         //int idTicket = 1;
       //Query query = em.createNamedQuery("Ticket.findByIdSitzplatz").setParameter("t.ticketPK.idSitzplatz",Ticket.class);
       //return query.toString();
            
        String yes = "Juhuuuu";
        return yes;
    }


           /**
     *
     * @param i
     * @return
     */
    public Kunde findKundeById(int i){
        try {
         return em.createNamedQuery("Kunde.findByIdKunde", Kunde.class)
                .setParameter("idKunde",i )
                .getSingleResult();
       // return kdn.getKdLatName().toString();
        }
        catch (NoResultException e) {
            return null;
        }
    }
           /**
            * Verkaufte Ticket mit Sitzplatznummer suchen
            * @param i
            * @return 
            */
    /**
     *
     * @param i
     * @return
     */
    public Object getTicketById(int i){
        
        //Query query = em.createNativeQuery("select * from FOOTBALL.TICKET join football.sitzplatz using(id_sitzplatz) join Football.sektor using(id_sektor) where FOOTBALL.SEKTOR.ID_SEKTOR = 1;");
       Query query= em.createNamedQuery("Ticket.findByIdSitzplatz").setParameter("idSitzplatz", i);
        return query.getSingleResult();
    }
           
    public List<Ticket> findAllTicket(){
        
        return em.createNamedQuery("Ticket.findAll", Ticket.class).getResultList();
       
    }
    
    /**
     *
     * @param idSitzplatz
     * @param idGames
     * @return
     */
    public String getKundenname(int idSitzplatz,int idGames ) {
        
       /* Ticket custID = new Ticket(idSitzplatz,idGames);
       
        Kunde kundenID =  custID.getIdKunde();
        String name = kundenID.getKdFirstName();
         */
       Query  query = em.createNamedQuery("FindTicketByKunde").setParameter("t.id_kunde",idSitzplatz);
        return query.toString();
        
        
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

}
